package com.wanczy.dao.impl;

import java.sql.Connection;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.wanczy.dao.MenuTableDAO;
import com.wanczy.pojo.MenuTable;

public class MenuTableDAOImpl implements MenuTableDAO{
	public Connection conn = null;
	public MenuTableDAOImpl(Connection conn) {
		this.conn =conn;
	}

	@Override
	public List<MenuTable> findMenu() {
		List<MenuTable> list = null;
		try {
			String sql = "select menu_id, menu_name, menu_describe, menu_state, menu_url, menu_level from menu_table where length(menu_level) = 3 and menu_state = 1  ";
			QueryRunner qRunner = new QueryRunner();
			list = qRunner.query(conn,sql,new BeanListHandler<>(MenuTable.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<MenuTable> findDownMenu(String menuLevel,int userId) {
		List<MenuTable> list = null;
		try {
			String sql = "select menu_id, menu_name, menu_describe, menu_state, menu_url, menu_level from menu_table where menu_level like ? and length(menu_level) = 6 and menu_state = 1 and " + 
					"menu_id in (select menu_id from role_menu_table where role_id in (select role_id from user_role_table where user_id = ?))";
			QueryRunner qRunner = new QueryRunner();
			Object params[] = {menuLevel+"%",userId};
			list = qRunner.query(conn,sql,new BeanListHandler<>(MenuTable.class),params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
